package com.example.demo.models.admin;

import com.example.demo.models.base.Authenticatable;
import com.example.demo.models.base.BaseEntity;
import com.example.demo.models.base.JWTSubject;
import com.example.demo.models.user.User;
import com.example.demo.services.admin.AdminService;
import com.example.demo.services.admin.UserService;
import gaarason.database.annotation.Column;
import gaarason.database.annotation.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.stereotype.Repository;

@Data
@ToString(callSuper = true)
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
@Table(name = "admins")
public class Admin extends BaseEntity implements JWTSubject, Authenticatable {
    private static final long serialVersionUID = 1L;

    /**
     * auto generator start
     **/
    @Column(name = "number", comment = "学生：学号 老师：工号")
    private String number;

    @Column(name = "name", nullable = true, comment = "姓名")
    private String name;

    @Override
    public String getJWTIdentifier() {
        return String.valueOf(this.getId());
    }

    @Override
    public String getJWTClassId() {
        return "13646365";
    }

    //判断是否有权限
    public boolean checkPermission(String method, String path) {
        return AdminService.checkAdminHasPermissionUrl(this, method, path);
    }

    public String getCollege() {
        User user = UserService.getUserData(this.getNumber());
        if(user!=null)
        {
            return user.getCollege();
        }
       return null;
    }

    /**
     * auto generator end
     **/
    @Repository
    public static class Model extends BaseModel<Admin, Long> {
        @Override
        protected boolean softDeleting() {
            return true;
        }
    }

}